copy_ssh_id(){
  cd
  if [ -f ".ssh/id_rsa.pub" ]; then
    ssh-copy-id -i .ssh/id_rsa.pub root@$1
  else
    ssh-keygen -t rsa
    ssh-copy-id -i .ssh/id_rsa.pub root@$1
  fi
}

replace_ip(){
  sed -ri 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b'/$1/ $2
  echo $1 >> /etc/ansible/hosts
}

check_input_para(){
  echo $1|grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" > /dev/null
  if [ $? = 0 ]; then
    return 0
  else
    return 1
  fi
}

inst_ansible(){
  dnf install epel-release -y
  dnf install ansible -y
  src_path=`pwd`
  mv /etc/ansible/hosts /etc/ansible/host.bak
  ln -s $src_path/hosts /etc/ansible/hosts
  cp ansible.cfg /etc/ansible/ansible.cfg
  grep interpreter_python /etc/ansible/ansible.cfg
  if [ $? -eq 1 ]; then
    sed -i '/\[defaults\]/a\interpreter_python = /usr/bin/python3' /etc/ansible/ansible.cfg
  fi
  #dnf install python3.12
  #rm /usr/bin/python3
  #ln -s /usr/bin/python3.12 /usr/bin/python3
  dnf install -y python3.9-pip
  ln -s /usr/bin/pip3.9 /usr/bin/pip3 
  pip3 install setuptools-rust
  pip3 install --upgrade pip
  pip3 install ansible
}

copy_ssh_id(){
  cd
  if [ -f ".ssh/id_rsa.pub" ]; then
    ssh-copy-id -i .ssh/id_rsa.pub root@$1
  else
    ssh-keygen -t rsa
    ssh-copy-id -i .ssh/id_rsa.pub root@$1
  fi
}

#src_path=`pwd`
#check_input_para $1
#copy_ssh_id $1
#cd $src_path
#replace_ip $1 "./install-docker.yml"
#ansible-playbook ./install-docker.yml

inst_ansible
for ip in `python3 get_ansible_host_group_msg.py allnode`
do
  copy_ssh_id $ip
done

